
/**
 * This class has method to create database and put some sample data
 * and can get connection from this class
 * @author LeDangHoa
 *
 */




package csc.traning.wpsj.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



public class DatabaseInit {

	
	 public static final String JDBC_URL ="jdbc:derby://localhost:1527/E:/LeDangHoa;create=true";
	 public static final String DRIVER = "org.apache.derby.jdbc.ClientDriver";
	  
	  public DatabaseInit()
	  {
		
	  }
	  
	  
	  public static Connection getConnection() throws  IllegalAccessException
	  {
		  Connection connection=null;
		  try {
			Class.forName(DRIVER);
			connection = DriverManager.getConnection(JDBC_URL);
		} catch (ClassNotFoundException cnfe) {
			// TODO Auto-generated catch block
			cnfe.printStackTrace();
		}catch (SQLException sqle) {
			sqle.printStackTrace();
		}
		  return connection;
	  }
	  private void createDatabase() throws ClassNotFoundException, SQLException
	  {
		  
		
			  Class.forName(DRIVER);
			  Connection connection = DriverManager.getConnection(JDBC_URL);
			 
		      Statement statement = connection.createStatement();
		      	    
		      //create table users
		      String tableAccount="CREATE TABLE USERS "
		      		+ "(USER_NAME VARCHAR(15) NOT NULL"
		      		+ " CONSTRAINT USER_PK PRIMARY KEY,"
		      		+ "FIRST_NAME VARCHAR(15),"
		      		+ "LAST_NAME VARCHAR(15),"
		      		+ "PASSWORD VARCHAR(15),"
		      		+ "EMAIL VARCHAR(15),"
		      		+ "CELLPHONE VARCHAR(15),"
		      		+ "ROLE VARCHAR(15))";
		      
				 statement.execute(tableAccount);
		      
		     
	      	    
	      	 
	      	    
	      	
	      

	  }
	public static void main(String[] args) throws SQLException, ClassNotFoundException {
	
		DatabaseInit db = new DatabaseInit();
		db.createDatabase();
		
	
	}
}
